import storage from './storage'
import api from './api'
import common from './common'
module.exports = {
	onlogin: function() {
		return new Promise((succ, error) => {
			let token = storage.getData('token')
			console.log('token',token)
			if (token) {
				succ.call(self, token)
				return;
			}
			var that=this
			let user = JSON.parse(storage.getData('user'))
			uni.login({
				provider: 'weixin',
				success(res) {
					console.log('login',res)
					that.post(api.user.login, {
						'username': user.username,
						'password':user.password
					}, 'form').then(res => {
						// console.log('res',res)
						if (res && res.code == 0) {
							console.log('save_token',res.data.token)
							storage.setData('token', res.data.token);
							storage.setJson('user', res.data);
							succ.call(self, res.data.token)//再次调用的promise方法判断是否有token
						}
						else{
							console.log('fail')
							uni.navigateTo({
								url: '../login/index'
							});
						}
					});
				}
			})
		});
	},
	postU: function(url, data, header,showinfo, back) {
		if(showinfo==undefined) showinfo=true
		header = header && header !== '' ? header : "application/json";
		if (header == "form") {
			header = "application/x-www-form-urlencoded"
		}
		uni.showLoading({
			title: '加载中'
		});
		var headers = {}
		headers["content-type"] = header
		let token = storage.getData("token");
		if (token) {
			headers['Authorization'] = token
		}
		headers['MinFrom'] = api.appid
		// console.log('url',api.getUrl(url),data,headers)
		return new Promise((succ, error) => {
			uni.request({
				url: api.getUrl(url),
				method: 'POST',
				data: data,
				header: headers,
				success: (res) => {
					if (res.statusCode == 200) {
						if (res.data.code) {
							if (res.data.code == 4444 && showinfo) {
								uni.showModal({
									title: '提示',
									content: res.data.msg,
									showCancel: false
								});
								if (back) succ.call(self, res.data)
							} else if (res.data.code == -1) {
								storage.setData('token','')
								// #ifdef MP-WEIXIN
								var my=self
								console.log('selef')
								uni.navigateTo({
									url: '../login/index'
								});
								// this.onlogin().then(t=>{
								// 	this.postU(url, data, header, back).then(lres=>{
								// 		succ.call(my, lres)
								// 	})
								// })
								// #endif
								// #ifndef MP-WEIXIN
								console.log('selef1')
								// uni.navigateTo({
								// 	url: '../login/index'
								// });
								// #endif
							}else if (res.data.code == -2) {
								console.log('selef2')
								uni.navigateTo({
									url: '../login/index'
								});
							}else {
								succ.call(self, res.data)
							}
						} else {
							succ.call(self, res.data)
						}
					} else {
						uni.showModal({
							title: '提示',
							content: '网络出错了'
						});
					}
				},
				fail(e) {
					uni.showModal({
						title: '提示',
						content: '网络出错了'
					});
					error.call(self, e)
				},
				complete: function() {
					uni.hideLoading();
				}
			});
		})
	},
	post: function(url, data, header,showinfo, back) {
		if(showinfo==undefined) showinfo=true
		header = header || "application/json";
		if (header == "form") header = "application/x-www-form-urlencoded"
		uni.showLoading({
			title: '加载中'
		});
		var headers = {}
		headers["content-type"] = header
		headers['MinFrom'] = api.appid
		console.log("getopen", api.getUrl(url),data,headers)
		return new Promise((succ, error) => {
			uni.request({
				url: url.indexOf('http') > -1 ? url : api.getUrl(url),
				method: 'POST',
				data: data,
				header: headers,
				success: (res) => {
					if (res.statusCode == 200) {
						if (res.data.code && res.data.code == 4444 && showinfo) {
							uni.showModal({
								title: '提示',
								content: res.data.msg,
								showCancel: false
							});
							if (back) succ.call(self, res.data)
						} else {
							succ.call(self, res.data)
						}
					} else {
						uni.showModal({
							title: '提示',
							content: '网络出错了'
						});
					}
				},
				fail(e) {
					uni.showModal({
						title: '提示',
						content: '网络出错了'
					});
					error.call(self, e)
				},
				complete: function() {
					uni.hideLoading();
				}
			});
		})

	},
	get: function(url, data, header, back) {
		header = header || "application/json";
		uni.showLoading({
			title: '加载中'
		});
		var headers = {}
		headers["content-type"] = header
		headers['MinFrom'] = api.appid
		console.log('111',url)
		return new Promise((succ, error) => {
			uni.request({
				url: url.indexOf('http') > -1 ? url : api.getUrl(url),
				method: 'GET',
				data: data,
				header: headers,
				success: function (res) {
					if (res.statusCode == 200) {
						if (res.data.code) {
							if (res.data.code == 4444) {
								uni.showModal({
									title: '提示',
									content: res.data.msg,
									showCancel: false
								});
								if (back) succ.call(self, res.data)
							} else {
								succ.call(self, res.data)
							}
						} else {
							succ.call(self, res.data)
						}
					} else {
						uni.showModal({
							title: '提示',
							content: '网络出错了'
						});
					}
				},
				fail: function (e) {
				    error.call(self, e)
				},
				complete: function() {
					uni.hideLoading();
				}
			});
		})

	},
	getU: function(url, data, header, back) {
		header = header && header !== '' ? header : "application/json";
		if (header == "form") header = "application/x-www-form-urlencoded"
		uni.showLoading({
			title: '加载中'
		});
		var headers = {}
		headers["content-type"] = header
		let token = storage.getData("token");
		if (token) {
			headers['Authorization'] = token
		}
		headers['MinFrom'] = api.appid
		return new Promise((succ, error) => {
			uni.request({
				url: api.getUrl(url),
				method: 'GET',
				data: data,
				header: headers,
				success: (res) => {
					if (res.statusCode == 200) {
						if (res.data.code) {
							if (res.data.code == 4444) {
								uni.showModal({
									title: '提示',
									content: res.data.msg,
									showCancel: false
								});
								if (back) succ.call(self, res.data)
							} else if (res.data.code == -1 || res.data.code == -2) {
								uni.navigateTo({
									url: '../login/index'
								});
							}else {
								succ.call(self, res.data)
							}
						} else {
							succ.call(self, res.data)
						}
					} else {
						uni.showModal({
							title: '提示',
							content: '网络出错了'
						});
					}
				},
				fail(e) {
					uni.showModal({
						title: '提示',
						content: '网络出错了'
					});
					error.call(self, e)
				},
				complete: function() {
					uni.hideLoading();
				}
			});
		})
	}
}
